package com.sendpo.zjx.sendpo.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.domain.WantdeliverDomain;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;
import com.sendpo.zjx.sendpo.views.MainScrollview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ch.ielse.view.imagewatcher.ImageWatcher;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2017/8/10.
 */

public class QiangOrderInfoActivity extends AppCompatActivity implements ImageWatcher.OnPictureLongPressListener {
    @BindView(R.id.backbt)
    RelativeLayout backbt;
    @BindView(R.id.head)
    RelativeLayout head;
    @BindView(R.id.bottom)
    RelativeLayout bottom;
    @BindView(R.id.jihuo_icon)
    TextView jihuoIcon;
    @BindView(R.id.info_icon)
    CircleImageView infoIcon;
    @BindView(R.id.info_title)
    TextView infoTitle;
    @BindView(R.id.info_realname)
    TextView infoRealname;
    @BindView(R.id.info_address)
    TextView infoAddress;
    @BindView(R.id.addresstv)
    TextView addresstv;
    @BindView(R.id.info_toaddress)
    TextView infoToaddress;
    @BindView(R.id.info_toaddress_tv)
    TextView infoToaddressTv;

    @BindView(R.id.info_payfor)
    TextView infoPayfor;
    @BindView(R.id.payfor_tv)
    TextView payforTv;
    @BindView(R.id.shenfen_title)
    TextView shenfenTitle;
    @BindView(R.id.zhengmian_iv)
    ImageView zhengmianIv;
    @BindView(R.id.zhengmian_bt)
    RelativeLayout zhengmianBt;
    @BindView(R.id.fanmian_iv)
    ImageView fanmianIv;
    @BindView(R.id.fanmian_bt)
    RelativeLayout fanmianBt;
    @BindView(R.id.maincrollview)
    MainScrollview maincrollview;
    @BindView(R.id.v_image_watcher)
    ImageWatcher vImageWatcher;
    @BindView(R.id.info_beizhu_tv)
    TextView infoBeizhuTv;
    // public List<Marker> overlayOptionses=null;
    private WantdeliverDomain.DataBean dataBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qiang_orderinfo);
        ButterKnife.bind(this);
        Tools.initStatus(this, R.color.black);
        dataBean = (WantdeliverDomain.DataBean) getIntent().getSerializableExtra("data");
        initComponent();
    }

    private void initComponent() {
        vImageWatcher.setTranslucentStatus(0); // 如果是透明状态栏，你需要给ImageWatcher标记 一个偏移值，以修正点击ImageView查看的启动动画的Y轴起点的不正确
        vImageWatcher.setErrorImageRes(R.mipmap.error_picture); // 配置error图标 如果不介意使用lib自带的图标，并不一定要调用这个API
        vImageWatcher.setOnPictureLongPressListener(this); // 长按图片的回调，你可以显示一个框继续提供一些复制，发送等功能
        if (dataBean != null) {
            Glide.with(this)
                    .load(dataBean.getUserAvatarpath())
                    .asBitmap()
                    .placeholder(getResources().getColor(R.color.default_lightgray))
                    .centerCrop()
                    .animate(R.anim.fade_in)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                            infoIcon.setImageBitmap(resource);
                        }
                    });
            infoIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", dataBean.getUserId());
                    Tools.redirectActivity(QiangOrderInfoActivity.this, UserPersonCenterActivity.class, map);
                }
            });
            infoRealname.setText(dataBean.getUserRealname().substring(0, 1) + "（先生/女士）");
            addresstv.setText(dataBean.getFromProvince() + dataBean.getFromCity() + dataBean.getFromArea() + dataBean.getFromAddress());
            infoToaddressTv.setText(dataBean.getToProvince() + dataBean.getToCity() + dataBean.getFromArea() + dataBean.getToAddress());
            if (dataBean.getWantdeliver_imgs() != null && dataBean.getWantdeliver_imgs().size() != 0) {
                final List<ImageView> ivs = new ArrayList<>();
                ivs.add(zhengmianIv);
                ivs.add(fanmianIv);
                Glide.with(this)
                        .load(dataBean.getWantdeliver_imgs().get(0))
                        .asBitmap()
                        .animate(R.anim.fade_in)
                        .centerCrop()
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                zhengmianIv.setImageBitmap(resource);
                            }
                        });
                zhengmianIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        vImageWatcher.show(zhengmianIv, ivs, dataBean.getWantdeliver_imgs());
                    }
                });
                Glide.with(this)
                        .load(dataBean.getWantdeliver_imgs().get(1))
                        .asBitmap()
                        .animate(R.anim.fade_in)
                        .centerCrop()
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                fanmianIv.setImageBitmap(resource);
                            }
                        });
                fanmianIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        vImageWatcher.show(fanmianIv, ivs, dataBean.getWantdeliver_imgs());
                    }
                });
            }
            switch (dataBean.getPayType()) {
                case 0:
                    payforTv.setText("寄付现结");
                    break;
                case 1:
                    payforTv.setText("货到付款");
                    break;


            }
            infoBeizhuTv.setText("" + dataBean.getUserContent());
        }
    }

    @OnClick(value = {R.id.backbt, R.id.call_phone})
    public void onMyClick(View view) {
        switch (view.getId()) {
            case R.id.backbt:
                finish();
                break;
            case R.id.call_phone:
                 List<User> userList= Xutils.queryAll(getApplication(),User.class);
                  if(userList==null||userList.size()==0){
                      Tools.redirectActivity(this,LoginActivity.class);
                      return;
                  }
                  if(userList.get(0).getUserRealnamestate()==0){
                      Toast.makeText(getApplicationContext(),"尚未实名认证",Toast.LENGTH_SHORT).show();
                      Tools.redirectActivity(this,UserShimingActivity.class);
                      return;
                  }else if(userList.get(0).getUserRealnamestate()==1){
                      Toast.makeText(getApplicationContext(),"实名认证中",Toast.LENGTH_SHORT).show();
                        return;
                  }else if(userList.get(0).getUserRealnamestate()==3){
                      Toast.makeText(getApplicationContext(),"实名认证失败",Toast.LENGTH_SHORT).show();
                      Tools.redirectActivity(this,UserShimingActivity.class);
                      return;
                  }


                if (dataBean != null) {
                    Intent start = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + dataBean.getUserIphone()));
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                        // TODO: Consider calling
                        //    ActivityCompat#requestPermissions
                        // here to request the missing permissions, and then overriding
                        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                        //                                          int[] grantResults)
                        // to handle the case where the user grants the permission. See the documentation
                        // for ActivityCompat#requestPermissions for more details.
                        Toast.makeText(getApplicationContext(),"请检查手机拨打权限",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    startActivity(start);
                  }
                break;
        }
    }

    @Override
    public void onPictureLongPress(ImageView v, String url, int pos) {

    }
}
